﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace JewererBasic.Models
{
    public class Customer
    {        
        public int CustID { get; set; }
        [Required]
        public string Name { get; set; }
        public string Address { get; set; }
        public string ContactNo { get; set; }
    }
    class CustomerSupply
    {
        static List<Customer> Customers;
        static public List<Customer> GetCustomers()
        {
            if (Customers == null)
            {
                Customers = new List<Customer>();
                for (int i = 0; i < 10; i++)
                    Customers.Add(new Customer() { CustID = i, Name = "cus" + i.ToString(), Address = "add" + i.ToString(), ContactNo = "090" + i.ToString() });
            }
            return Customers;
        }
    }

    public class PagedCustomerModel
    {
        public int TotalRows { get; set; }
        public List<Customer> Customers { get; set; }
        public int PageSize { get; set; }

        public PagedCustomerModel()
        {
            Customers = CustomerSupply.GetCustomers();
            TotalRows = CustomerSupply.GetCustomers().Count;
            PageSize = 9;
        }
        public bool SaveCustomer(string name, string address, string contactno)
        {
            try
            {
                Customer cust = new Customer();
                cust.Name = name;
                cust.Address = address;
                cust.ContactNo = contactno;
                cust.CustID = TotalRows + 1;
                CustomerSupply.GetCustomers().Add(cust);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool UpdateCustomer(int id, string name, string address, string contactno)
        {
            try
            {
                for (int i = 0; i < 10; i++)
                {
                    if (Customers[i].CustID == id)
                    {
                        Customer cust = Customers[i];
                        cust.Name = name;
                        cust.Address = address;
                        cust.ContactNo = contactno;
                    }
                }                
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DeleteCustomer(int id)
        {
            try
            {
                for (int i = 0; i < 10; i++)
                {
                    if (Customers[i].CustID == id)
                    {
                        Customers.RemoveAt(i);
                        break;
                    }
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}